package com.cjk.console.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.cjk.console.request.StbLogRequest;
import com.cjk.foundation.commons.response.PageResponse;
import com.cjk.foundation.commons.response.Response;
import com.cjk.foundation.dao.DeviceDao;
import com.cjk.foundation.dao.GroupDao;
import com.cjk.foundation.dao.LogDao;
import com.cjk.foundation.dao.WatchLogDao;
import com.cjk.foundation.entity.DeviceChangeLogEntity;
import com.cjk.foundation.entity.DeviceEntity;
import com.cjk.foundation.entity.FlowLogEntity;
import com.cjk.foundation.entity.GroupEntity;
import com.cjk.foundation.entity.SysInfoLogEntity;
import com.cjk.foundation.entity.WatchLogEntity;
import com.cjk.foundation.enums.DeviceChangeLogType;

@Service
public class LogServiceImpl implements LogService {

	private static final Logger LOGGER = LoggerFactory.getLogger(LogServiceImpl.class);
	
	@Autowired
	private GroupDao groupDao;

	@Autowired
	private DeviceDao deviceDao;

	@Autowired
	private WatchLogDao watchLogDao;

	@Autowired
	private LogDao logDao;
	
	@Override
	public Response getCurrentChannelPlayNum(StbLogRequest request, Integer userId) {
		GroupEntity groupEntity = groupDao.selectById(request.getGroupId());
		List<WatchLogEntity> watchLogEntities = watchLogDao.selectCurrentChannelPlayNum(userId, request.getGroupId(), request.getStartTime());
		List<String> channels = new ArrayList<>();
		List<Integer> nums = new ArrayList<>();
		for (WatchLogEntity watchLogEntity : watchLogEntities) {
			channels.add(watchLogEntity.getChannelName());
			nums.add(watchLogEntity.getNum());
		}
		Map<String, Object> result = new HashMap<>();
		String group = groupEntity == null ? "所有分组" : groupEntity.getName();
		result.put("group", group);
		result.put("channels", channels);
		result.put("nums", nums);
		return Response.success(result);
	}
	
	@Override
	public Response getCurrentSysInfoLog(String sn) {
		return Response.success(logDao.selectLastBySn(sn));
	}

	@Override
	public Response getFlowLog(String sn) {
		return Response.success(logDao.selectFlowLogBySn(sn));
	}
	
	@Override
	public PageResponse getWatchLogList(StbLogRequest request, Integer userId) {
		List<WatchLogEntity> watchLogEntities = watchLogDao.selectListByCondition(request.getSn(),
				userId, request.getGroupId(), request.getStartTime(), request.getEndTime(), request.getPage(), request.getPageSize());
		if (CollectionUtils.isEmpty(watchLogEntities)) {
			return PageResponse.success(0, request.getPage(), new ArrayList<>());
		}
		Integer total = watchLogDao.selectCountByCondition(request.getSn(), userId, request.getGroupId(), request.getStartTime(),
				request.getEndTime());
		return PageResponse.success(total, request.getPage(), watchLogEntities);
	}
	
	
	
	
	
	

	@Override
	public void insertDeviceChangeLog(String sn, String room, String tenantName, DeviceChangeLogType logType,
			Date date) {
		DeviceChangeLogEntity deviceChangeLogEntity = new DeviceChangeLogEntity();
		deviceChangeLogEntity.setSn(sn);
		deviceChangeLogEntity.setRoom(room);
		deviceChangeLogEntity.setTenantName(tenantName);
		deviceChangeLogEntity.setLogType(logType);
		deviceChangeLogEntity.setCreateTime(date);
		watchLogDao.insertDeviceChangeLog(deviceChangeLogEntity);
	}

	@Override
	public void insertSysInfoLog(SysInfoLogEntity sysInfoLogEntity) {
		DeviceEntity deviceEntity = deviceDao.selectBySn(sysInfoLogEntity.getSn(), null);
		if (deviceEntity == null) {
			LOGGER.warn("insert sysinfo log device {} is not exist", sysInfoLogEntity.getSn());
			return;
		}
		// SysInfoLogEntity sysInfoLogEntity = new SysInfoLogEntity();
		// sysInfoLogEntity.setSn(sn);
		// sysInfoLogEntity.setProcessNum(processNum);
		// sysInfoLogEntity.setCpuRate(cpuRate);
		// sysInfoLogEntity.setMemoryRate(memoryRate);
		// sysInfoLogEntity.setUploadTime(uploadTime);
		logDao.insertSysInfoLog(sysInfoLogEntity);
	}

	@Override
	public void insertFlowLog(FlowLogEntity flowLogEntity) {
		DeviceEntity deviceEntity = deviceDao.selectBySn(flowLogEntity.getSn(), null);
		if (deviceEntity == null) {
			LOGGER.warn("insert flow log device {} is not exist", flowLogEntity.getSn());
			return;
		}
		// FlowLogEntity flowLogEntity = new FlowLogEntity();
		// flowLogEntity.setSn(sn);
		// flowLogEntity.setNetworkCard(networkCard);
		// flowLogEntity.setRecv(recv);
		// flowLogEntity.setSend(send);
		// flowLogEntity.setUploadTime(uploadTime);
		logDao.insertFlowLog(flowLogEntity);
	}

}
